package com.ruoyi.personal.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.personal.domain.User;
import com.ruoyi.personal.service.IUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 个人信息管理Controller
 *
 * @author qjp
 * @date 2024-09-11
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;

    /**
     * 查询个人信息管理列表
     */
    @RequiresPermissions("personal:user:list")
    @GetMapping("/list")
    public TableDataInfo list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出个人信息管理列表
     */
    @RequiresPermissions("personal:user:export")
    @Log(title = "个人信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "个人信息管理数据");
    }

    /**
     * 获取个人信息管理详细信息
     */
    @RequiresPermissions("personal:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增个人信息管理
     */
    @RequiresPermissions("personal:user:add")
    @Log(title = "个人信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改个人信息管理
     */
    @RequiresPermissions("personal:user:edit")
    @Log(title = "个人信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除个人信息管理
     */
    @RequiresPermissions("personal:user:remove")
    @Log(title = "个人信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.deleteUserByIds(ids));
    }


    /**
     * 修改个人信息管理 同步ruoyi的个人中心
     */
    @RequiresPermissions("personal:user:edit")
    @Log(title = "个人信息管理", businessType = BusinessType.UPDATE)
    @PutMapping("/editByNumid")
    public AjaxResult editByNumid(@RequestBody User user) {
        System.out.println(user);
        return toAjax(userService.updateUserByNumid(user));
    }

    /**
     * 添加代理人 根据 id department name 查询
     */
//    @RequiresPermissions("personal:user:")
//    @Log(title = "个人信息管理", businessType = BusinessType.INSERT)
    @PostMapping("/selectOneUser")
    public AjaxResult selectOneUser(@RequestBody User user) {
        System.out.println(user.toString());
        int i = userService.selectOneUser(user);
        System.out.println("222222222222222222222222222222222");
        System.out.println(i);
        System.out.println("222222222222222222222222222222222");
        return success(i);
    }

    /*
    * 统计打卡人数
    *
    */
    @GetMapping("/record")
    public AjaxResult selectRecord() {
        return success(userService.selectUserRecordList());
    }


}
